package com.itheima.web.controller.store;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;


@WebServlet("/store/company")
public class CompanyServlet extends BaseServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)) {
            findAll(req, resp);
        } else if ("toAdd".equals(operation)) {
            toAdd(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            update(req, resp);
        }
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.getWriter().write("<script>alert('上传失败!')</script>");
//        if(true){return;}
        int pageNum = 1;
        int pageSize = 5;
        if (StringUtils.isNoneBlank(req.getParameter("page"))) {
            pageNum = Integer.parseInt(req.getParameter("page"));
        }
        if (StringUtils.isNoneBlank(req.getParameter("size"))) {
            pageSize = Integer.parseInt(req.getParameter("size"));
        }

        PageInfo all = companyService.findAll(pageNum, pageSize);
        req.setAttribute("page", all);

        req.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(req, resp);

    }


    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Company company = BeanUtil.fillBean(req, Company.class, "yyyyy-MM-dd");


        companyService.save(company);
        resp.sendRedirect(req.getContextPath() + "/store/company?operation=list");

    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(req, resp);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String[] id = req.getParameter("id").split(",");
        companyService.delete(id);
        resp.sendRedirect(req.getContextPath() + "/store/company?operation=list");

    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Company company = companyService.findById(id);
        req.setAttribute("company", company);
        req.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Company company =  BeanUtil.fillBean(req,Company.class,"yyyy-MM-dd");
        companyService.update(company);
        resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
    }


    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
